package com.huangmb.anymark.app.fragment.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.*;
import android.widget.TextView;
import com.alibaba.fastjson.JSON;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.huangmb.androidlib.utils.Logger;
import com.huangmb.anymark.app.R;
import com.huangmb.anymark.app.activity.UserCenterActivity;
import com.huangmb.anymark.app.adapter.UserCenterMarkerAdapter;
import com.huangmb.anymark.app.api.BaseApi;
import com.huangmb.anymark.app.api.UserMarkerApi;
import com.huangmb.anymark.app.api.UsersApi;
import com.huangmb.anymark.app.app.AnyMarkApplication;
import com.huangmb.anymark.app.fragment.BaseMapFragment;
import com.huangmb.anymark.app.fragment.marker.FoodShopFragment;
import com.huangmb.anymark.app.model.User;
import com.huangmb.anymark.app.model.UserMarker;
import com.huangmb.anymark.app.model.UserContext;
import com.huangmb.anymark.app.ui.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hmb
 * @version 1.0
 * @since 2016/4/19 14:38
 */
public class UserCenterFragment extends BaseMapFragment {
    private static final String TAG = UserCenterFragment.class.getSimpleName();
    private UserContext mUserContext;
    private SuperRecyclerView mRecyclerView;
    private UserCenterMarkerAdapter mAdapter;
    private final List<UserMarker> mUserMarkerList = new ArrayList<>();
    private List<Marker> mMarkerList;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_user_center, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        User user = AnyMarkApplication.getInstance().getUserContext().getUser();
        getActionBar().setTitle(user.getName()+"的标注");
        mRecyclerView = (SuperRecyclerView) view.findViewById(R.id.list);
        mAdapter = new UserCenterMarkerAdapter((UserCenterActivity) getActivity(),mUserMarkerList);
        mRecyclerView.setAdapter(mAdapter);

        MapView mapView = (MapView) view.findViewById(R.id.map);
        setUpMapView(mapView, savedInstanceState);

        UserMarkerApi.myList(new BaseApi.AbsListener() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                List<UserMarker> list = JSON.parseArray(result, UserMarker.class);
                mUserMarkerList.clear();
                if (list != null && !list.isEmpty()) {
                    mUserMarkerList.addAll(list);
                    mAdapter.notifyDataSetChanged();
                    //在地图上添加标记
                    mAMap.clear();
                    ArrayList<MarkerOptions> optionsList = new ArrayList<>();

                    int i = 0;
                    for (UserMarker item : list) {
                        i++;
                        LatLng pos = new LatLng(item.getLatitude(), item.getLongitude());
                        MarkerOptions options = new MarkerOptions();
                        options.position(pos);
                        options.title(item.getTitle());
                        TextView icon = (TextView) getActivity().getLayoutInflater().inflate(R.layout.marker,null);
                        icon.setText(""+i);
                        options.icon(BitmapDescriptorFactory.fromView(icon));
                        optionsList.add(options);

                    }
                    Logger.d(TAG, "绘制标记数目" + optionsList.size());
                    mMarkerList = mAMap.addMarkers(optionsList, false);

                }
            }
        });

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.usercenter_menu,menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.logout){
            UsersApi.logout();
            toast("退出成功");
            getActivity().finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
